package com.moon.interview.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jd.platform.hotkey.client.callback.JdHotKeyStore;
import com.moon.interview.common.BaseResponse;
import com.moon.interview.common.ResultUtils;
import com.moon.interview.exception.SysErrorCode;
import com.moon.interview.constant.HotKeyConstant;
import com.moon.interview.exception.ApplicationException;
import com.moon.interview.model.dto.questionBank.QuestionBankAddRequest;
import com.moon.interview.model.dto.questionBank.QuestionBankQueryRequest;
import com.moon.interview.model.dto.questionBank.QuestionBankUpdateRequest;
import com.moon.interview.model.entity.QuestionBank;
import com.moon.interview.model.vo.QuestionBankVo;
import com.moon.interview.sentinel.SentinelGlobalHandler;
import com.moon.interview.service.IQuestionBankService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/questionBank")
@Tag(name = "题库")
public class QuestionBankController {
    @Resource
    private IQuestionBankService questionBankService;

    @Operation(summary = "分页查询题库 - 用户")
    @PostMapping("/page")
    public BaseResponse<Page<QuestionBank>> page(@RequestBody QuestionBankQueryRequest request) {
        return ResultUtils.success(questionBankService.page(request));
    }

    @Operation(summary = "分页查询题库详情")
    @PostMapping("/page/allVo")
    @SentinelResource(value = "pageVoByUserId", blockHandlerClass = SentinelGlobalHandler.class, blockHandler = "handleBlockException",fallback = "handleFallback")
    public BaseResponse<Page<QuestionBankVo>> pageAllVo(@RequestBody QuestionBankQueryRequest request) {
        return ResultUtils.success(questionBankService.pageQuestionBankVo(request));
    }

    @Operation(summary = "分页查询当前用户的题库详情")
    @PostMapping("/page/vo")
    public BaseResponse<Page<QuestionBankVo>> pageVoByUserId(@RequestBody QuestionBankQueryRequest queryRequest) {
        return ResultUtils.success(questionBankService.pageQuestionBankVoByUserId(queryRequest));
    }

    @Operation(summary = "新增题库")
    @PostMapping("/add")
    public BaseResponse add(@RequestBody QuestionBankAddRequest addRequest) {
        if (addRequest == null) {
            throw new ApplicationException(SysErrorCode.PARAMS_ERROR);
        }
        return ResultUtils.success(questionBankService.addQuestionBank(addRequest));
    }

    @Operation(summary = "更新题库")
    @PostMapping("/update")
    public BaseResponse update(@RequestBody QuestionBankUpdateRequest updateRequest) {
        if (updateRequest == null) {
            throw new ApplicationException(SysErrorCode.PARAMS_ERROR);
        }
        return ResultUtils.success(questionBankService.updateQuestionBank(updateRequest));
    }

    @Operation(summary = "删除题库")
    @GetMapping("/remove")
    public BaseResponse remove(@RequestParam Long id) {
        if (id == null) {
            throw new ApplicationException(SysErrorCode.PARAMS_ERROR);
        }
        return ResultUtils.success(questionBankService.removeQuestionBank(id));
    }

    @Operation(summary = "根据id获取题库详情（包括题目列表）")
    @GetMapping("/detail/simple")
    public BaseResponse<QuestionBankVo> getBankDetailWithSimpleQuestionById (@RequestParam Long id) {
        if (id == null) {
            throw new ApplicationException(SysErrorCode.PARAMS_ERROR);
        }
        String key = HotKeyConstant.getUserSignInRedisKey(id);
        // 判断是否是热点  上报hotkey记录访问次数
        if (JdHotKeyStore.isHotKey(key)) {
            // 获取缓存值
            Object cacheBankVo = JdHotKeyStore.get(key);
            if (cacheBankVo != null) {
                return ResultUtils.success((QuestionBankVo) cacheBankVo);
            }
        }
        // 不是热点key，从数据库获取数据
        QuestionBankVo bankVo = questionBankService.getBankDetailWithSimpleQuestionById(id);
        // 设置本地缓存 是热点key设置本地缓存，反之不设置，也不会上报给hotkey
        JdHotKeyStore.smartSet(key, bankVo);
        return ResultUtils.success(bankVo);
    }

    @Operation(summary = "根据id获取题库")
    @GetMapping("/get")
    public BaseResponse getQuestionBankById (@RequestParam Long id) {
        if (id == null) {
            throw new ApplicationException(SysErrorCode.PARAMS_ERROR);
        }
        return ResultUtils.success(questionBankService.getById(id));
    }
}
